home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 2134 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.1 KB

  1. From: SJSELS@msn.com (Leigh Saunders)
  2. Subject: RE: integer-2-string
  3. Date: 19 Jan 96 05:09:59 -0800
  4. References: <4dht24$g0g@ratree.psu.ac.th>
  5. Message-ID: <00001a80+00006eb6@msn.com>
  6. Path: news.msn.com!msn.com
  7. Newsgroups: comp.lang.c
  8. Organization: The Microsoft Network (msn.com)
  9.  
  10. >,Hi there,
  11.  
  12. >  In C on UNIX,how can I convert integer to string ??
  13. >Function itoa() doesn't work.......
  14.  
  15. Well if itoa() doesn't do it for you I would use something like this:
  16.  
  17. char *int_to_ascii(int *value)
  18. {
  19.     char *temp=NULL;
  20.  
  21.     temp=malloc(6);
  22.     sprintf(temp,"%-5d",value);
  23.     return(temp);
  24. }
  25. The calling function will have to be responsible for free()'ing the 
  26. returned string.  Some small changes can be done to make a more 
  27. generic and portable function by using a char[] of a max length of a 
  28. #define that coresponds with the max digits of the max int on the 
  29. system it is compiled on and using that to store the string and then 
  30. get it's size with strlen() and malloc the char *temp to pass out of 
  31. the function.  The above example is very crude and would only work if 
  32. you knew that 5 digits was the max digits that value would ever 
  33. contain.
  34.